{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Remove Comments"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: removeComments"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #删除注释"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给一个 C++ 程序，删除程序中的注释。这个程序<code>source</code>是一个数组，其中<code>source[i]</code>表示第&nbsp;<code>i</code>&nbsp;行源码。&nbsp;这表示每行源码由 <code>'\\n'</code>&nbsp;分隔。</p>\n",
    "\n",
    "<p>在 C++ 中有两种注释风格，行内注释和块注释。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>字符串<code>//</code> 表示行注释，表示<code>//</code>和其右侧的其余字符应该被忽略。</li>\n",
    "\t<li>字符串<code>/*</code> 表示一个块注释，它表示直到下一个（非重叠）出现的<code>*/</code>之间的所有字符都应该被忽略。（阅读顺序为从左到右）非重叠是指，字符串<code>/*/</code>并没有结束块注释，因为注释的结尾与开头相重叠。</li>\n",
    "</ul>\n",
    "\n",
    "<p>第一个有效注释优先于其他注释。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>如果字符串<code>//</code>出现在块注释中会被忽略。</li>\n",
    "\t<li>同样，如果字符串<code>/*</code>出现在行或块注释中也会被忽略。</li>\n",
    "</ul>\n",
    "\n",
    "<p>如果一行在删除注释之后变为空字符串，那么<strong>不要</strong>输出该行。即，答案列表中的每个字符串都是非空的。</p>\n",
    "\n",
    "<p>样例中<strong>没有</strong>控制字符，单引号或双引号字符。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>比如，<code>source = \"string s = \"/* Not a comment. */\";\"</code> 不会出现在测试样例里。</li>\n",
    "</ul>\n",
    "\n",
    "<p>此外，没有其他内容（如定义或宏）会干扰注释。</p>\n",
    "\n",
    "<p>我们保证每一个块注释最终都会被闭合， 所以在行或块注释之外的<code>/*</code>总是开始新的注释。</p>\n",
    "\n",
    "<p>最后，隐式换行符<strong>可以</strong>通过块注释删除。 有关详细信息，请参阅下面的示例。</p>\n",
    "\n",
    "<p>从源代码中删除注释后，需要以相同的格式返回源代码。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例&nbsp;1:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> source = [\"/*Test program */\", \"int main()\", \"{ \", \"  // variable declaration \", \"int a, b, c;\", \"/* This is a test\", \"   multiline  \", \"   comment for \", \"   testing */\", \"a = b + c;\", \"}\"]\n",
    "<strong>输出:</strong> [\"int main()\",\"{ \",\"  \",\"int a, b, c;\",\"a = b + c;\",\"}\"]\n",
    "<strong>解释:</strong> 示例代码可以编排成这样:\n",
    "/*Test program */\n",
    "int main()\n",
    "{ \n",
    "  // variable declaration \n",
    "int a, b, c;\n",
    "/* This is a test\n",
    "   multiline  \n",
    "   comment for \n",
    "   testing */\n",
    "a = b + c;\n",
    "}\n",
    "第 1 行和第 6-9 行的字符串 /* 表示块注释。第 4 行的字符串 // 表示行注释。\n",
    "编排后: \n",
    "int main()\n",
    "{ \n",
    "  \n",
    "int a, b, c;\n",
    "a = b + c;\n",
    "}</pre>\n",
    "\n",
    "<p><strong>示例 2:</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入:</strong> source = [\"a/*comment\", \"line\", \"more_comment*/b\"]\n",
    "<strong>输出:</strong> [\"ab\"]\n",
    "<strong>解释:</strong> 原始的 source 字符串是 \"a/*comment<strong>\\n</strong>line<strong>\\n</strong>more_comment*/b\", 其中我们用粗体显示了换行符。删除注释后，隐含的换行符被删除，留下字符串 \"ab\" 用换行符分隔成数组时就是 [\"ab\"].\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示:</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= source.length &lt;= 100</code></li>\n",
    "\t<li><code>0 &lt;= source[i].length &lt;= 80</code></li>\n",
    "\t<li><code>source[i]</code>&nbsp;由可打印的 <strong>ASCII</strong> 字符组成。</li>\n",
    "\t<li>每个块注释都会被闭合。</li>\n",
    "\t<li>给定的源码中不会有单引号、双引号或其他控制字符。</li>\n",
    "</ul>\n",
    "<span style=\"display:block\"><span style=\"height:0px\"><span style=\"position:absolute\"><span style=\"top:0px\"><span style=\"left:-9999px\"><span style=\"opacity:0\"><span style=\"overflow:hidden\">&nbsp;</span></span></span></span></span></span>​​​​​​</span>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [remove-comments](https://leetcode.cn/problems/remove-comments/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [remove-comments](https://leetcode.cn/problems/remove-comments/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"/*Test program */\", \"int main()\", \"{ \", \"  // variable declaration \", \"int a, b, c;\", \"/* This is a test\", \"   multiline  \", \"   comment for \", \"   testing */\", \"a = b + c;\", \"}\"]', '[\"a/*comment\", \"line\", \"more_comment*/b\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        t = []\n",
    "        block_comment = False\n",
    "        for s in source:\n",
    "            i, m = 0, len(s)\n",
    "            while i < m:\n",
    "                if block_comment:\n",
    "                    if i + 1 < m and s[i : i + 2] == \"*/\":\n",
    "                        block_comment = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < m and s[i : i + 2] == \"/*\":\n",
    "                        block_comment = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < m and s[i : i + 2] == \"//\":\n",
    "                        break\n",
    "                    else:\n",
    "                        t.append(s[i])\n",
    "                i += 1\n",
    "            if not block_comment and t:\n",
    "                ans.append(\"\".join(t))\n",
    "                t.clear()\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        t = []\n",
    "        block_comment = False\n",
    "        for s in source:\n",
    "            i, m = 0, len(s)\n",
    "            while i < m:\n",
    "                if block_comment:\n",
    "                    if i + 1 < m and s[i : i + 2] == \"*/\":\n",
    "                        block_comment = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < m and s[i : i + 2] == \"/*\":\n",
    "                        block_comment = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < m and s[i : i + 2] == \"//\":\n",
    "                        break\n",
    "                    else:\n",
    "                        t.append(s[i])\n",
    "                i += 1\n",
    "            if not block_comment and t:\n",
    "                ans.append(\"\".join(t))\n",
    "                t.clear()\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        t = []\n",
    "        block_comment = False\n",
    "        for s in source:\n",
    "            i, m = 0, len(s)\n",
    "            while i < m:\n",
    "                if block_comment:\n",
    "                    if i + 1 < m and s[i : i + 2] == \"*/\":\n",
    "                        block_comment = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < m and s[i : i + 2] == \"/*\":\n",
    "                        block_comment = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < m and s[i : i + 2] == \"//\":\n",
    "                        break\n",
    "                    else:\n",
    "                        t.append(s[i])\n",
    "                i += 1\n",
    "            if not block_comment and t:\n",
    "                ans.append(\"\".join(t))\n",
    "                t.clear()\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        t = []\n",
    "        ans = []\n",
    "        flag = False\n",
    "        \n",
    "        for s in source:\n",
    "            i, m = 0, len(s)\n",
    "            while i < m:\n",
    "                if flag:\n",
    "                    if i+1 < m and s[i:i+2] == \"*/\":\n",
    "                        flag = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i+1<m and s[i:i+2] == \"/*\":\n",
    "                        flag = True\n",
    "                        i += 1\n",
    "                    elif i+1 < m and s[i:i+2] == \"//\":\n",
    "                        break\n",
    "                    else:\n",
    "                        t.append(s[i])\n",
    "                i+= 1\n",
    "            if not flag and t:\n",
    "                ans.append(\"\".join(t))\n",
    "                t.clear()\n",
    "        return ans \n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        # 匹配所有 // 和 /* */，后者用非贪婪模式。将所有匹配结果替换成空串。最后移除多余空行。\n",
    "        return list(filter(None, re.sub('//.*|/\\*(.|\\n)*?\\*/', '', '\\n'.join(source)).split('\\n')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        res=[]\n",
    "        new=[]\n",
    "        flag=False\n",
    "        n=len(source)\n",
    "        for line in source:\n",
    "            i=0\n",
    "            while i<len(line):\n",
    "                if flag:\n",
    "                    if i+1<len(line) and line[i]=='*' and line[i+1]=='/':\n",
    "                        flag=False\n",
    "                        i+=1\n",
    "                else:\n",
    "                    if i+1<len(line) and line[i]=='/' and line[i+1]=='*':\n",
    "                        flag=True\n",
    "                        i+=1\n",
    "                    elif i+1<len(line) and line[i]=='/' and line[i+1]=='/':\n",
    "                        break\n",
    "                    else:\n",
    "                        new.append(line[i])\n",
    "                i+=1\n",
    "            if not flag and len(new)>0:\n",
    "                res.append(''.join(new))\n",
    "                new=[]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        in_block = False\n",
    "        cur_res = \"\"\n",
    "        res = []\n",
    "        for line in source:\n",
    "            n = len(line)\n",
    "            idx = 0\n",
    "            while idx < n:\n",
    "                if in_block:\n",
    "                    if line[idx:idx+2] == \"*/\":\n",
    "                        in_block = False \n",
    "                        idx += 2\n",
    "                    else:\n",
    "                        idx += 1\n",
    "                else:\n",
    "                    if line[idx:idx+2] == \"//\":\n",
    "                        break\n",
    "                    elif line[idx:idx+2] == \"/*\":\n",
    "                        in_block = True \n",
    "                        idx += 2\n",
    "                    else:\n",
    "                        cur_res += line[idx]\n",
    "                        idx += 1\n",
    "            if not in_block and cur_res:\n",
    "                res.append(cur_res)\n",
    "                cur_res = \"\"\n",
    "        return res \n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        res = []\n",
    "        new_line = []\n",
    "        in_block = False\n",
    "        for line in source:\n",
    "            i = 0\n",
    "            while i < len(line):\n",
    "                if in_block:\n",
    "                    if i + 1 < len(line) and line[i] == '*' and line[i + 1] == '/':\n",
    "                        in_block = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < len(line) and line[i] == '/' and line[i + 1] == '*':\n",
    "                        in_block = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < len(line) and line[i] == '/' and line[i + 1] == '/':\n",
    "                        break\n",
    "                    else:\n",
    "                        new_line.append(line[i])\n",
    "                i += 1\n",
    "                \n",
    "            if  not in_block and len(new_line) > 0:\n",
    "                res.append(''.join(new_line))\n",
    "                new_line = []\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "      return list(filter(None, re.sub(r\"//.*|/\\*(.|\\n)*?\\*/\", \"\", \"\\n\".join(source)).split(\"\\n\")))\n",
    "        \n",
    "\n",
    "        \n",
    "      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        res = []\n",
    "        new_line = []\n",
    "        in_block = False\n",
    "        for line in source:\n",
    "            # 开始遍历\n",
    "            i = 0\n",
    "            while i < len(line):\n",
    "                if in_block: # 在块中\n",
    "                    if i + 1 < len(line) and line[i:i+2] == '*/': # 结束块\n",
    "                        in_block  = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    # 不在块中\n",
    "                    if i + 1 < len(line) and line[i:i+2] == '/*': # 开始块\n",
    "                        in_block = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < len(line) and line[i:i+2] == '//': # 注释\n",
    "                        break\n",
    "                    else:\n",
    "                        new_line.append(line[i])\n",
    "                i += 1\n",
    "            if not in_block and len(new_line) > 0:\n",
    "                res.append(\"\".join(new_line))\n",
    "                new_line = []\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        code = '\\n'.join(source)\n",
    "        patten = re.compile('//.*|/\\*(.|\\n)*?\\*/')\n",
    "        res = re.sub(patten,'',code).split('\\n')\n",
    "        ret = []\n",
    "        for x in res:\n",
    "            if x:\n",
    "                ret.append(x)\n",
    "        return ret\n",
    "        #return list(filter(None,))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\r\n",
    "        ret=[]\r\n",
    "        cur=[]\r\n",
    "        inblock=False\r\n",
    "        for line in source:\r\n",
    "            idx=0\r\n",
    "            L=len(line)\r\n",
    "            while idx<L:\r\n",
    "                if inblock:\r\n",
    "                    if line[idx]=='*' and idx+1<L and line[idx+1]=='/':\r\n",
    "                        inblock=False\r\n",
    "                        idx+=1\r\n",
    "                elif line[idx]=='/' and idx+1<L:\r\n",
    "                    if line[idx+1]=='*':\r\n",
    "                        inblock=True\r\n",
    "                        idx+=1\r\n",
    "                    elif line[idx+1]=='/':\r\n",
    "                        break\r\n",
    "                    else:\r\n",
    "                        cur.append(line[idx])\r\n",
    "                else:\r\n",
    "                    cur.append(line[idx])\r\n",
    "                idx+=1\r\n",
    "            if not inblock and len(cur):\r\n",
    "                ret.append(''.join(cur))\r\n",
    "                cur=[]\r\n",
    "            \r\n",
    "            \r\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        lst = []\n",
    "        s = \"\\n\".join(source)\n",
    "        # print(s)\n",
    "\n",
    "        s = re.sub('//.*|/\\*(.|\\n)*?\\*/','',s)\n",
    "        # print(s)\n",
    "        lst = s.split('\\n')\n",
    "        p = []\n",
    "        for i in lst:\n",
    "            if i:\n",
    "                print(i)\n",
    "                p.append(i)\n",
    "        return p\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        s=\"\\n\".join(source)+\"\\n\"\n",
    "        n=len(s)\n",
    "        ans=\"\"\n",
    "        i=0\n",
    "        while i<n:\n",
    "            if s[i:i+2]==\"//\":\n",
    "                i=s.find(\"\\n\",i+2)\n",
    "            elif s[i:i+2]==\"/*\":\n",
    "                i=s.find(\"*/\",i+2)+2\n",
    "            else:\n",
    "                ans+=s[i]\n",
    "                i+=1\n",
    "        return [i for i in ans.split(\"\\n\") if i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        line = ''\n",
    "        ans = []\n",
    "        comment = False\n",
    "        for s in source:\n",
    "            i = 0\n",
    "            while i < len(s):\n",
    "                if not comment:\n",
    "                    if s[i] == '/' and i + 1 < len(s) and s[i + 1] == '/':\n",
    "                        break\n",
    "                    elif s[i] == '/' and i + 1 < len(s) and s[i + 1] == '*':\n",
    "                        i += 1\n",
    "                        comment = True\n",
    "                    else:\n",
    "                        line += s[i]\n",
    "                else:\n",
    "                    if s[i] == '*' and i + 1 < len(s) and s[i + 1] == '/':\n",
    "                        i += 1\n",
    "                        comment = False\n",
    "                \n",
    "                i += 1\n",
    "            if line and not comment:\n",
    "                ans.append(line)\n",
    "                line = ''\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        res = []\n",
    "        new_line = []\n",
    "        in_block = False\n",
    "        for line in source:\n",
    "            i = 0\n",
    "            while i < len(line):\n",
    "                if in_block:\n",
    "                    if i + 1 < len(line) and line[i] == '*' and line[i + 1] == '/':\n",
    "                        in_block = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < len(line) and line[i] == '/' and line[i + 1] == '*':\n",
    "                        in_block = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < len(line) and line[i] == '/' and line[i + 1] == '/':\n",
    "                        break\n",
    "                    else:\n",
    "                        new_line.append(line[i])\n",
    "                i += 1\n",
    "                \n",
    "            if  not in_block and len(new_line) > 0:\n",
    "                res.append(''.join(new_line))\n",
    "                new_line = []\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        flag = False\n",
    "        ans = []\n",
    "        prt = \"\"\n",
    "        for line in source:\n",
    "            n = len(line)\n",
    "            i = 0\n",
    "            start, end = 0, n\n",
    "            while i < n:\n",
    "                if not flag:\n",
    "                    if line[i] == '/' and i < n - 1:\n",
    "                        if line[i+1] == '/':\n",
    "                            end = i\n",
    "                            break\n",
    "                        elif line[i+1] == '*':\n",
    "                            prt += line[start:i]\n",
    "                            flag = True\n",
    "                            i += 2\n",
    "                            continue\n",
    "                else:\n",
    "                    if line[i] == '*' and i < n - 1:\n",
    "                        if line[i+1] == '/':\n",
    "                            flag = False\n",
    "                            i += 2\n",
    "                            start = i\n",
    "                            continue\n",
    "                i += 1\n",
    "            if not flag:\n",
    "                prt += line[start: end]\n",
    "                if prt:\n",
    "                    ans.append(prt)\n",
    "                    prt = \"\"\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        res = []\n",
    "        new_line = []\n",
    "        in_block = False\n",
    "        for line in source:\n",
    "            i = 0\n",
    "            while i < len(line):\n",
    "                if in_block:\n",
    "                    if i + 1 < len(line) and line[i] == '*' and line[i + 1] == '/':\n",
    "                        in_block = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < len(line) and line[i] == '/' and line[i + 1] == '*':\n",
    "                        in_block = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < len(line) and line[i] == '/' and line[i + 1] == '/':\n",
    "                        break\n",
    "                    else:\n",
    "                        new_line.append(line[i])\n",
    "                i += 1\n",
    "                \n",
    "            if  not in_block and len(new_line) > 0:\n",
    "                res.append(''.join(new_line))\n",
    "                new_line = []\n",
    "        return res\n",
    "\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        # 匹配所有 // 和 /* */，后者用非贪婪模式。将所有匹配结果替换成空串。最后移除多余空行。\n",
    "        return list(filter(None, re.sub('//.*|/\\*(.|\\n)*?\\*/', '', '\\n'.join(source)).split('\\n')))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        # 难点在分类讨论\n",
    "        res = []\n",
    "        new_line = []\n",
    "        in_block = False\n",
    "        for line in source:\n",
    "            i = 0\n",
    "            while i < len(line):\n",
    "                if in_block:\n",
    "                    if i + 1 < len(line) and line[i] == '*' and line[i + 1] == '/':\n",
    "                        in_block = False\n",
    "                        i += 1 # 最后还有一个统一的i+=1\n",
    "                else:\n",
    "                    if i + 1 < len(line) and line[i] == '/' and line[i + 1] == '*':\n",
    "                        in_block = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < len(line) and line[i] == '/' and line[i + 1] == '/':\n",
    "                        break\n",
    "                    else:\n",
    "                        new_line.append(line[i])\n",
    "                i += 1\n",
    "                \n",
    "            if  not in_block and len(new_line) > 0:\n",
    "                # 判断not in_block是因为跨行的/**/需要合并\n",
    "                res.append(''.join(new_line))\n",
    "                new_line = []\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        \n",
    "        index = 0\n",
    "        while index< len(source):\n",
    "            cur_source = source[index]\n",
    "            for i in range(len(cur_source)-1):\n",
    "                # print(cur_source[i:i+2])\n",
    "                if cur_source[i:i+2] == \"//\":\n",
    "                    source[index] = source[index][:i]\n",
    "                    if not source[index]:\n",
    "                        source.pop(index)\n",
    "                    break\n",
    "                elif (cur_source[i:i+2] == \"/*\") :\n",
    "                    if (\"*/\" in cur_source[i+2:]):\n",
    "                        for j in range(i+2, len(cur_source)-1):\n",
    "                            if cur_source[j:j+2] == \"*/\":\n",
    "                                source[index] = source[index][:i]+source[index][j+2:]\n",
    "                                break\n",
    "                    else:\n",
    "                        before_source = source[index][:i]\n",
    "                        while index<len(source):\n",
    "                            source.pop(index)\n",
    "                            if \"*/\" in source[index]:\n",
    "                                break\n",
    "\n",
    "                        for j in range(len(source[index])-1):\n",
    "                            if source[index][j:j+2] == \"*/\":\n",
    "                                source[index] = before_source+source[index][j+2:]\n",
    "                                break\n",
    "                    if not source[index]:\n",
    "                        source.pop(index)\n",
    "                    break\n",
    "            else:\n",
    "                index += 1\n",
    "\n",
    "\n",
    "        return source\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "\n",
    "        res, new_line, in_block = [], [], False\n",
    "\n",
    "        for line in source:\n",
    "            i = 0\n",
    "            while i < len(line):\n",
    "                if in_block:\n",
    "                    if i + 1 < len(line) and line[i] == '*' and line[i + 1] == '/':\n",
    "                        in_block = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < len(line) and line[i] == '/' and line[i + 1] == '*':\n",
    "                        in_block = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < len(line) and line[i] == '/' and line[i + 1] == '/':\n",
    "                        break\n",
    "                    else:\n",
    "                        new_line.append(line[i])\n",
    "                i += 1\n",
    "\n",
    "            if not in_block and len(new_line) > 0:\n",
    "                res.append(''.join(new_line))\n",
    "                new_line.clear()\n",
    "                \n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        res_list = []\n",
    "        long_annot_flag = 0\n",
    "        tmp_code = ''\n",
    "        for i in source:\n",
    "            tmp_n = len(i)\n",
    "            idx = 0\n",
    "            while idx < tmp_n:\n",
    "                if long_annot_flag == 0:\n",
    "                    if i[idx] == '/':\n",
    "                        if idx == tmp_n - 1:   #此时位于行末\n",
    "                            tmp_code += i[idx]\n",
    "                            break\n",
    "                        if i[ idx + 1 ] == '/':\n",
    "                            break\n",
    "                        elif i[ idx + 1  ] == '*':\n",
    "                            long_annot_flag = 1\n",
    "                            idx += 1\n",
    "                        else:\n",
    "                            tmp_code += i[idx]\n",
    "                    else:\n",
    "                        tmp_code += i[idx]\n",
    "                else:   #处于长片段注释中\n",
    "                    if i[idx] == '*':\n",
    "                        if i[ min(idx + 1 , tmp_n-1)  ] == '/':    #长片段注释结束\n",
    "                            long_annot_flag = 0\n",
    "                            idx += 1\n",
    "                idx += 1\n",
    "            if len(tmp_code) > 0 and long_annot_flag == 0:\n",
    "                res_list.append(tmp_code)\n",
    "                tmp_code = ''\n",
    "        return res_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        t = []\n",
    "        block_comment = False\n",
    "        for s in source:\n",
    "            i, m = 0, len(s)\n",
    "            while i < m:\n",
    "                if block_comment:\n",
    "                    if i + 1 < m and s[i : i + 2] == \"*/\":\n",
    "                        block_comment = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < m and s[i : i + 2] == \"/*\":\n",
    "                        block_comment = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < m and s[i : i + 2] == \"//\":\n",
    "                        break\n",
    "                    else:\n",
    "                        t.append(s[i])\n",
    "                i += 1\n",
    "            if not block_comment and t:\n",
    "                ans.append(\"\".join(t))\n",
    "                t.clear()\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        t = []\n",
    "        hangzhushi = False\n",
    "        for s in source:\n",
    "            i = 0\n",
    "            m = len(s)\n",
    "            while i<m:\n",
    "                if hangzhushi:\n",
    "                    if i+1<m and s[i:i+2]=='*/':\n",
    "                        hangzhushi = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i+1<m and s[i:i+2]=='/*':\n",
    "                        hangzhushi = True\n",
    "                        i += 1\n",
    "                    elif i+1<m and s[i:i+2]=='//':\n",
    "                        break\n",
    "                    else:\n",
    "                        t.append(s[i])\n",
    "                i += 1\n",
    "            if not hangzhushi and t:\n",
    "                ans.append(\"\".join(t))\n",
    "                t.clear()\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        flag = False\n",
    "        t = []\n",
    "        for x in source:\n",
    "            i, m = 0, len(x)\n",
    "            while i<m:\n",
    "\n",
    "                if flag:\n",
    "                    if i+1<m and x[i:i+2] == '*/':\n",
    "                        flag = False\n",
    "                        i+=1\n",
    "                \n",
    "                else:\n",
    "                    if i + 1<m and x[i:i+2] == '/*':\n",
    "                        flag = True\n",
    "                        i+=1\n",
    "                    elif i+1 < m and x[i:i+2] == '//':\n",
    "                        break\n",
    "                    else:\n",
    "                        t.append(x[i])\n",
    "                i+=1\n",
    "                \n",
    "            if not flag and t:\n",
    "                ans.append(''.join(t))\n",
    "                t.clear()\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        OP = []\n",
    "\n",
    "        bk = 1\n",
    "        s=''\n",
    "        sbk=''\n",
    "        for ln in source:\n",
    "            for ch in ln:\n",
    "                if bk:\n",
    "                    if ch == '/':\n",
    "                        if len(s)>0 and s[-1] == '/':\n",
    "                            s=s[:-1]\n",
    "                            break\n",
    "                        s += ch\n",
    "                    elif ch=='*':\n",
    "                        if len(s)>0 and s[-1] =='/':\n",
    "                            s=s[:-1]\n",
    "                            bk=0\n",
    "                            sbk = 'abc'\n",
    "                        if bk:\n",
    "                            s += ch\n",
    "                    else:\n",
    "                        s += ch\n",
    "                else:\n",
    "                    if ch=='/':\n",
    "                        if sbk[-1] == '*':\n",
    "                            bk=1\n",
    "                            sbk = 'abc'\n",
    "                    sbk += ch\n",
    "\n",
    "            if bk:\n",
    "                s += \"/n\"\n",
    "        \n",
    "        print(s)\n",
    "        OP = s.split(\"/n\")\n",
    "        ii=0\n",
    "        while ii<len(OP):\n",
    "            if OP[ii] == '':\n",
    "                OP.pop(ii)\n",
    "                ii -= 1\n",
    "            ii += 1\n",
    "\n",
    "        return OP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        # 匹配所有 // 和 /* */，后者用非贪婪模式。将所有匹配结果替换成空串。最后移除多余空行。\n",
    "        return list(filter(None, re.sub('//.*|/\\*(.|\\n)*?\\*/', '', '\\n'.join(source)).split('\\n')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def removeComments(self, source: List[str]) -> List[str]:\n",
    "#         s = '\\n'.join(source) + '\\n' #为最后一行的'//'提供闭区间\n",
    "#         i, n = 1, len(s)\n",
    "#         ans = ''\n",
    "#         while i < n:\n",
    "#             if s[i - 1] + s[i] == '//':\n",
    "#                 i = s.find('\\n', i) + 1\n",
    "#             elif s[i - 1] + s[i] == '/*':\n",
    "#                 i = s.find('*/', i + 1) + 3\n",
    "#             else:\n",
    "#                 ans += s[i - 1]\n",
    "#                 i += 1\n",
    "#         return filter(len, ans.split('\\n'))\n",
    "\n",
    "# class Solution:\n",
    "#     def removeComments(self, source: List[str]) -> List[str]:\n",
    "#         s = re.sub('//.*|/\\*(\\s|.)*?\\*/', '', '\\n'.join(source)) \n",
    "#         return filter(len, s.split('\\n'))\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        return list(filter(None, re.sub('//.*|/\\*(.|\\n)*?\\*/', '', '\\n'.join(source)).split('\\n')))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        a, s = 0, \"\"\n",
    "        dc = {0:{\"/\":[1,0], \"#\":[0,1]}, 1:{\"/\":[2,0], \"*\":[3,0], \"#\":[0,2]}, 2:{\"'\":[0,1], \"#\":[2,0]}, 3:{\"*\":[4,0], \"#\":[3,0]}, 4:{\"/\":[0,0], \"*\":[4,0], \"#\":[3,0]}}\n",
    "        for i in \"'\".join(source):\n",
    "            a, x = dc[a][i if i in dc[a] else \"#\"]\n",
    "            if x: s += (\"/\" if x == 2 else \"\") + i\n",
    "        return [i for i in s.split(\"'\") if i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        new_line = []\n",
    "        in_block = False\n",
    "        for line in source:\n",
    "            i = 0\n",
    "            while i<len(line):\n",
    "                if in_block:\n",
    "                    if i+1<len(line) and line[i]=='*' and line[i+1]=='/':\n",
    "                        in_block = False\n",
    "                        i+=1\n",
    "                else:\n",
    "                    if i+1<len(line) and line[i]=='/' and line[i+1]=='*':\n",
    "                        in_block = True\n",
    "                        i+=1\n",
    "                    elif i+1<len(line) and line[i]=='/' and line[i+1]=='/':\n",
    "                        break\n",
    "                    else:\n",
    "                        new_line.append(line[i])\n",
    "                i+=1\n",
    "            if not in_block and new_line:\n",
    "                ans.append(''.join(new_line))\n",
    "                new_line = []\n",
    "        return ans\n",
    "\n",
    "                    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        t = []\n",
    "        block_comment = False\n",
    "        for s in source:\n",
    "            i, m = 0, len(s)\n",
    "            while i < m:\n",
    "                if block_comment:\n",
    "                    if i + 1 < m and s[i : i + 2] == \"*/\":\n",
    "                        block_comment = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < m and s[i : i + 2] == \"/*\":\n",
    "                        block_comment = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < m and s[i : i + 2] == \"//\":\n",
    "                        break\n",
    "                    else:\n",
    "                        t.append(s[i])\n",
    "                i += 1\n",
    "            if not block_comment and t:\n",
    "                ans.append(\"\".join(t))\n",
    "                t.clear()\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        # 匹配所有 // 和 /* */，后者用非贪婪模式。将所有匹配结果替换成空串。最后移除多余空行。\n",
    "        return list(filter(None, re.sub('//.*|/\\*(.|\\n)*?\\*/', '', '\\n'.join(source)).split('\\n')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import re\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        return list(filter(None, re.sub('//.*|/\\*(.|\\n)*?\\*/', '', '\\n'.join(source)).split('\\n')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        res = []\n",
    "        new_line = []\n",
    "        in_block = False\n",
    "        for line in source:\n",
    "            i = 0\n",
    "            while i < len(line):\n",
    "                if in_block:\n",
    "                    if i + 1 < len(line) and line[i] == '*' and line[i + 1] == '/':\n",
    "                        in_block = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < len(line) and line[i] == '/' and line[i + 1] == '*':\n",
    "                        in_block = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < len(line) and line[i] == '/' and line[i + 1] == '/':\n",
    "                        break\n",
    "                    else:\n",
    "                        new_line.append(line[i])\n",
    "                i += 1\n",
    "                \n",
    "            if  not in_block and len(new_line) > 0:\n",
    "                res.append(''.join(new_line))\n",
    "                new_line = []\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        ret = []\n",
    "        new_line = []\n",
    "        in_block = False\n",
    "        for line in source:\n",
    "            i = 0\n",
    "            while i < len(line):\n",
    "                if in_block:\n",
    "                    if i + 1 < len(line) and line[i] == '*' and line[i + 1] == '/':\n",
    "                        in_block = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < len(line) and line[i] == '/' and line[i + 1] == '*':\n",
    "                        in_block = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < len(line) and line[i] == '/' and line[i + 1] == '/':\n",
    "                        break\n",
    "                    else:\n",
    "                        new_line.append(line[i])\n",
    "                i += 1\n",
    "            \n",
    "            if not in_block and len(new_line) > 0:\n",
    "                ret.append(''.join(new_line))\n",
    "                new_line = []\n",
    "\n",
    "        return ret\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        t = []\n",
    "        block_comment = False \n",
    "        for s in source:\n",
    "            i,m= 0 ,len(s)\n",
    "            while i < m:\n",
    "                if block_comment:\n",
    "                    if i + 1 < m and s[i:i+2] =='*/':\n",
    "                        block_comment = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < m and s[i: i+2] == '//':\n",
    "                        break \n",
    "                    elif i + 1 < m and s[i:i+2] == '/*':\n",
    "                        block_comment = True \n",
    "                        i += 1\n",
    "                    else:\n",
    "                        t.append(s[i])\n",
    "                i += 1\n",
    "            if not block_comment and t:\n",
    "                ans.append(''.join(t))\n",
    "                t.clear()\n",
    "        return ans \n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        ans, t = [], []\n",
    "        block = False\n",
    "        for s in source:\n",
    "            i, m = 0, len(s)\n",
    "            while i < m:\n",
    "                if block:\n",
    "                    if i + 1 < m and s[i:i+2] == '*/':\n",
    "                        block = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < m and s[i:i+2] == '/*':\n",
    "                        block = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < m and s[i:i+2] == '//':\n",
    "                        break\n",
    "                    else:\n",
    "                        t.append(s[i])\n",
    "                i += 1\n",
    "            if not block and t:\n",
    "                ans.append(''.join(t))\n",
    "                t.clear()\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        t = []\n",
    "        block_comment = False\n",
    "        for s in source:\n",
    "            i, m = 0, len(s)\n",
    "            while i < m:\n",
    "                if block_comment:\n",
    "                    if i + 1 < m and s[i : i + 2] == \"*/\":\n",
    "                        block_comment = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < m and s[i : i + 2] == \"/*\":\n",
    "                        block_comment = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < m and s[i : i + 2] == \"//\":\n",
    "                        break\n",
    "                    else:\n",
    "                        t.append(s[i])\n",
    "                i += 1\n",
    "            if not block_comment and t:\n",
    "                ans.append(\"\".join(t))\n",
    "                t.clear()\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\r\n",
    "        ret=[]\r\n",
    "        cur=[]\r\n",
    "        block_comment=False\r\n",
    "        for line in source:\r\n",
    "            idx=0\r\n",
    "            if block_comment:\r\n",
    "                idx=line.find('*/')\r\n",
    "                if idx<0:\r\n",
    "                    continue\r\n",
    "                idx+=2\r\n",
    "                block_comment=False\r\n",
    "            L=len(line)\r\n",
    "            while idx<L:\r\n",
    "                # line_comment=line.find('//',idx)\r\n",
    "\r\n",
    "                if line[idx]=='/':\r\n",
    "                    if idx+1==L or line[idx+1] not in '/*':\r\n",
    "                        cur.append(line[idx])\r\n",
    "                        idx+=1\r\n",
    "                        continue\r\n",
    "                    if line[idx+1]=='/':\r\n",
    "                        if len(cur):\r\n",
    "                            ret.append(''.join(cur))\r\n",
    "                            cur=[]\r\n",
    "                        break\r\n",
    "                    if line[idx+1]=='*':\r\n",
    "                        block_comment=True\r\n",
    "                        nxt=line.find('*/',idx+2)\r\n",
    "                        if nxt>=0:\r\n",
    "                            idx=nxt+2\r\n",
    "                            block_comment=False\r\n",
    "                        else:\r\n",
    "                            break\r\n",
    "\r\n",
    "                else:\r\n",
    "                    cur.append(line[idx])\r\n",
    "                    idx+=1\r\n",
    "            else:\r\n",
    "                if len(cur):\r\n",
    "                    ret.append(''.join(cur))\r\n",
    "                    cur=[]\r\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        a, s = 0, \"\"\n",
    "        dc = [{\"/\":[1,0], \"#\":[0,1]}, {\"/\":[2,0], \"*\":[3,0], \"#\":[0,2]}, {\"'\":[0,1], \"#\":[2,0]}, {\"*\":[4,0], \"#\":[3,0]}, {\"/\":[0,0], \"*\":[4,0], \"#\":[3,0]}]\n",
    "        for i in \"'\".join(source):\n",
    "            a, x = dc[a][i if i in dc[a] else \"#\"]\n",
    "            if x: s += (\"/\" if x == 2 else \"\") + i\n",
    "        return [i for i in s.split(\"'\") if i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        t = []\n",
    "        block_comment = False\n",
    "        for s in source:\n",
    "            i, m = 0, len(s)\n",
    "            while i < m:\n",
    "                if block_comment:\n",
    "                    if i + 1 < m and s[i : i + 2] == \"*/\":\n",
    "                        block_comment = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < m and s[i : i + 2] == \"/*\":\n",
    "                        block_comment = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < m and s[i : i + 2] == \"//\":\n",
    "                        break\n",
    "                    else:\n",
    "                        t.append(s[i])\n",
    "                i += 1\n",
    "            if not block_comment and t:\n",
    "                ans.append(\"\".join(t))\n",
    "                t.clear()\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        res = []\n",
    "        new_line = []\n",
    "        in_block = False\n",
    "        for line in source:\n",
    "            i = 0\n",
    "            while i < len(line):\n",
    "                if in_block:\n",
    "                    if i + 1 < len(line) and line[i] == '*' and line[i + 1] == '/':\n",
    "                        in_block = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < len(line) and line[i:i+2] == \"/*\":\n",
    "                        in_block = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < len(line) and line[i:i+2] == \"//\":\n",
    "                        break\n",
    "                    else:\n",
    "                        new_line.append(line[i])\n",
    "                i += 1 \n",
    "            if not in_block and len(new_line) > 0:\n",
    "                res.append(''.join(new_line))\n",
    "                new_line = []\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        # 匹配所有 // 和 /* */，后者用非贪婪模式。将所有匹配结果替换成空串。最后移除多余空行。\n",
    "        return list(filter(None, re.sub('//.*|/\\*(.|\\n)*?\\*/', '', '\\n'.join(source)).split('\\n')))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        in_block = False\n",
    "        res = []\n",
    "        for line in source:\n",
    "            if not in_block:\n",
    "                new_line = []\n",
    "            i = 0\n",
    "            while i < len(line):\n",
    "                if not in_block and i < len(line) - 1 and line[i : i + 2] == \"/*\":\n",
    "                    in_block = True\n",
    "                    i += 2\n",
    "                elif in_block and i < len(line) - 1 and line[i:i+2] == \"*/\":\n",
    "                    in_block = False\n",
    "                    i += 2\n",
    "                elif not in_block and i < len(line) - 1 and line[i:i+2] == \"//\":\n",
    "                    break\n",
    "                else:\n",
    "                    if not in_block:\n",
    "                        print(i, line[i])\n",
    "                        new_line.append(line[i])\n",
    "                    i += 1\n",
    "            if not in_block and new_line:\n",
    "                res.append(''.join(new_line))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "      return list(filter(None, re.sub(r\"//.*|/\\*(.|\\n)*?\\*/\", \"\", \"\\n\".join(source)).split(\"\\n\")))\n",
    "        \n",
    "\n",
    "        \n",
    "      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        # 难点在分类讨论\n",
    "        res = []\n",
    "        new_line = []\n",
    "        in_block = False\n",
    "        for line in source:\n",
    "            i = 0\n",
    "            while i < len(line):\n",
    "                if in_block:\n",
    "                    if i + 1 < len(line) and line[i] == '*' and line[i + 1] == '/':\n",
    "                        in_block = False\n",
    "                        i += 1 # 最后还有一个统一的i+=1\n",
    "                else:\n",
    "                    if i + 1 < len(line) and line[i] == '/' and line[i + 1] == '*':\n",
    "                        in_block = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < len(line) and line[i] == '/' and line[i + 1] == '/':\n",
    "                        break\n",
    "                    else:\n",
    "                        new_line.append(line[i])\n",
    "                i += 1\n",
    "                \n",
    "            if  not in_block and len(new_line) > 0:\n",
    "                res.append(''.join(new_line))\n",
    "                new_line = []\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        a, s = 0, \"\"\n",
    "        dc = {0:{\"/\":[1,0], \"#\":[0,1]}, 1:{\"/\":[2,0], \"*\":[3,0], \"#\":[0,2]}, 2:{\"'\":[0,1], \"#\":[2,0]}, 3:{\"*\":[4,0], \"#\":[3,0]}, 4:{\"/\":[0,0], \"*\":[4,0], \"#\":[3,0]}}\n",
    "        for i in \"'\".join(source):\n",
    "            a, x = dc[a][i if i in dc[a] else \"#\"]\n",
    "            if x: s += (\"/\" if x == 2 else \"\") + i\n",
    "        return [i for i in s.split(\"'\") if i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        in_comment = False\n",
    "        result = []\n",
    "        ans = []\n",
    "\n",
    "        for line in source:\n",
    "            i = 0\n",
    "            while i < len(line):\n",
    "                if in_comment:\n",
    "                    if i + 1 < len(line) and line[i] == '*' and line[i + 1] == '/':\n",
    "                        in_comment = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < len(line) and line[i] == '/' and line[i + 1] == '*':\n",
    "                        in_comment = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < len(line) and line[i] == '/' and line[i + 1] == '/':\n",
    "                        break\n",
    "                    else:\n",
    "                        result.append(line[i])\n",
    "                i += 1\n",
    "\n",
    "            if not in_comment and len(result) > 0:\n",
    "                ans.append(''.join(result))\n",
    "                result = []\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "    \n",
    "    \n",
    "        res = []\n",
    "\n",
    "        new_line = []\n",
    "\n",
    "        inblock =False\n",
    "\n",
    "        for line in source:\n",
    "            i =0\n",
    "            n = len(line)\n",
    "            while i <n:\n",
    "                if inblock:\n",
    "                    if i + 1<n and line[i] == '*' and line[i+1] == '/':\n",
    "                        inblock = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if  i + 1<n and line[i] == '/' and line[i+1] == '*':\n",
    "                        inblock = True\n",
    "                        i += 1\n",
    "                    elif i + 1<n and line[i] == '/' and line[i+1] == '/':\n",
    "                        break\n",
    "                    else:\n",
    "                        new_line.append(line[i])\n",
    "                i += 1\n",
    "            if not inblock and len(new_line) > 0:\n",
    "                res.append(\"\".join(new_line))\n",
    "                new_line = []\n",
    "        return res\n",
    "        \n",
    "\n",
    "\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        a, s, ss = 0, \"\", \"'\".join(source)\n",
    "        dc = {0:{\"/\":[1,0], \"#\":[0,1]}, 1:{\"/\":[2,0], \"*\":[3,0], \"#\":[0,2]}, 2:{\"'\":[0,1], \"#\":[2,0]}, 3:{\"*\":[4,0], \"#\":[3,0]}, 4:{\"/\":[0,0], \"*\":[4,0], \"#\":[3,0]}}\n",
    "        for i in ss:\n",
    "            a, x = dc[a][i if i in dc[a] else \"#\"]\n",
    "            if x: s += (\"/\" if x == 2 else \"\") + i\n",
    "        return [i for i in s.split(\"'\") if i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        a, s = 0, \"\"\n",
    "        lis = [\"\", \"\", \"/\"]\n",
    "        dc = {0:{\"/\":[1,1], \"#\":[0,0]}, 1:{\"/\":[2,1], \"*\":[3,1], \"#\":[0,2]}, 2:{\"'\":[0,0], \"#\":[2,1]}, 3:{\"*\":[4,1], \"#\":[3,1]}, 4:{\"/\":[0,1], \"*\":[4,1], \"#\":[3,1]}}\n",
    "        for i in \"'\".join(source):\n",
    "            a, x = dc[a][i if i in dc[a] else \"#\"]\n",
    "            if x != 1:\n",
    "                s += lis[x] + i\n",
    "        return [i for i in s.split(\"'\") if i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        res = []\n",
    "        new_line = []\n",
    "        in_block = False\n",
    "        for line in source:\n",
    "            i = 0\n",
    "            while i < len(line):\n",
    "                if in_block:\n",
    "                    if i + 1 < len(line) and line[i] == '*' and line[i + 1] == '/':\n",
    "                        in_block = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < len(line) and line[i] == '/' and line[i + 1] == '*':\n",
    "                        in_block = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < len(line) and line[i] == '/' and line[i + 1] == '/':\n",
    "                        break\n",
    "                    else:\n",
    "                        new_line.append(line[i])\n",
    "                i += 1\n",
    "                \n",
    "            if  not in_block and len(new_line) > 0:\n",
    "                res.append(''.join(new_line))\n",
    "                new_line = []\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        new_line = []\n",
    "        block = False\n",
    "        for line in source:\n",
    "            i = 0\n",
    "            while i < len(line):\n",
    "                if block:\n",
    "                    if i + 1 < len(line) and line[i] =='*' and line[i+1] =='/':\n",
    "                        block = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < len(line) and line[i] == '/' and line[i+1] == '*':\n",
    "                        block = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < len(line) and line[i] == '/' and line[i+1] == '/':\n",
    "                        break\n",
    "                    else:\n",
    "                        new_line.append(line[i])\n",
    "                i+= 1\n",
    "            if not block and len(new_line) > 0:\n",
    "                ans.append(''.join(new_line))\n",
    "                new_line = []\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        return list(filter(None, re.sub('//.*|/\\*(.|\\n)*?\\*/', '', '\\n'.join(source)).split('\\n')))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        res = []\n",
    "        new_line = []\n",
    "        in_block = False\n",
    "        for line in source:\n",
    "            i = 0\n",
    "            while i < len(line):\n",
    "                if in_block:\n",
    "                    if i + 1 < len(line) and line[i] == '*' and line[i + 1] == '/':\n",
    "                        in_block = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < len(line) and line[i] == '/' and line[i + 1] == '*':\n",
    "                        in_block = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < len(line) and line[i] == '/' and line[i + 1] == '/':\n",
    "                        break\n",
    "                    else:\n",
    "                        new_line.append(line[i])\n",
    "                i += 1\n",
    "\n",
    "            if not in_block and len(new_line) > 0:\n",
    "                res.append(''.join(new_line))\n",
    "                new_line = []\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def isBank(self, s: str):\n",
    "\n",
    "        for i in range(s.__len__()):\n",
    "            if s[i] != \" \":\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "\n",
    "        ans = []\n",
    "\n",
    "        star = False\n",
    "        ban = False\n",
    "        delete = 0\n",
    "        for s in source:\n",
    "            j = 0\n",
    "            tmp = []\n",
    "            while j < s.__len__():\n",
    "\n",
    "                if ban:\n",
    "                    j += 1\n",
    "                    continue\n",
    "\n",
    "                if star and s[j] != \"*\":\n",
    "                    j += 1\n",
    "                    continue\n",
    "\n",
    "                if s[j] == '/' and j + 1 < s.__len__() and s[j + 1] in ['/', '*']:\n",
    "                    if s[j + 1] == '/':\n",
    "                        ban = True\n",
    "                    else:\n",
    "                        star = True\n",
    "                    j += 2\n",
    "                elif s[j] == '*':\n",
    "                    if j + 1 < s.__len__() and s[j + 1] in ['/']:\n",
    "                        if star:\n",
    "                            star = False\n",
    "                            delete = -delete\n",
    "                            j += 2\n",
    "                        else:\n",
    "                            tmp.append(s[j])\n",
    "                            j += 1\n",
    "                    else:\n",
    "                        if not star:\n",
    "                            tmp.append('*')\n",
    "                        j += 1\n",
    "                else:\n",
    "                    if not (star or ban):\n",
    "                        tmp.append(s[j])\n",
    "                    j += 1\n",
    "\n",
    "            if ban:\n",
    "                ban = False\n",
    "            if star:\n",
    "                delete += 1\n",
    "\n",
    "            t = \"\".join(tmp)\n",
    "            if t != \"\":\n",
    "                if delete < 0 and ans.__len__() >= 1:\n",
    "                    ans[ans.__len__() - 1] += t\n",
    "                    ans.append(\"\")\n",
    "                else:\n",
    "                    if ans.__len__() > 0:\n",
    "                        ans[ans.__len__() - 1] += t\n",
    "                    else:\n",
    "                        ans.append(t)\n",
    "                    if not star:\n",
    "                        ans.append(\"\")\n",
    "            if delete < 0:\n",
    "                if t == \"\" and s.__len__() >= 2 and s[-2:] == '*/' and ans.__len__() > 0 and ans[ans.__len__() - 1] != \"\":\n",
    "                    ans.append(\"\")\n",
    "                delete = 0\n",
    "        if ans.__len__() >= 1 and ans[ans.__len__() - 1] == \"\":\n",
    "            ans.pop(ans.__len__() - 1)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source):\n",
    "        isMutilple = False\n",
    "        ans = []\n",
    "        singleSource = '断'.join(source)+'断'\n",
    "        workPoint = 0\n",
    "        sin, mul = 0, 0\n",
    "        while (singleSource.find('//', workPoint)!=-1) or (singleSource.find('/*', workPoint)!=-1):\n",
    "            sin = singleSource.find('//', workPoint)\n",
    "            mul = singleSource.find('/*', workPoint)\n",
    "            rowEnd = singleSource.find('断', sin)\n",
    "            if 0<=sin<mul or (sin!=-1 and mul==-1):\n",
    "                singleSource = singleSource[0:sin]+singleSource[rowEnd:]\n",
    "                workPoint = sin\n",
    "            elif 0<=mul<sin or (mul!=-1 and sin==-1):\n",
    "                mulEnd = singleSource.find('*/', mul+2)\n",
    "                singleSource = singleSource[0:mul]+singleSource[mulEnd+2:]\n",
    "                workPoint = mul\n",
    "        #删除空行\n",
    "        r = singleSource.find('断断')\n",
    "        while r!=-1:\n",
    "            singleSource = singleSource[0:r]+singleSource[r+1:]\n",
    "            r = singleSource.find('断断')\n",
    "        if singleSource[0]=='断':\n",
    "            singleSource = singleSource[1:]\n",
    "        ans = singleSource.split('断')\n",
    "        ans.pop()\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        ans = []\n",
    "        new_line = []\n",
    "        is_block = False\n",
    "        for line in source:\n",
    "            idx = 0\n",
    "            while idx < len(line):\n",
    "                if is_block:\n",
    "                    if (\n",
    "                        idx + 1 < len(line)\n",
    "                        and line[idx] == \"*\"\n",
    "                        and line[idx + 1] == \"/\"\n",
    "                    ):\n",
    "                        # 这里表示可以摆脱注释区域\n",
    "                        is_block = False\n",
    "                        idx += 1\n",
    "                else:\n",
    "                    if (\n",
    "                        idx + 1 < len(line)\n",
    "                        and line[idx] == \"/\"\n",
    "                        and line[idx + 1] == \"*\"\n",
    "                    ):\n",
    "                        is_block = True\n",
    "                        idx += 1\n",
    "                    elif idx + 1 < len(line) and line[idx] == line[idx + 1] == \"/\":\n",
    "                        # 碰到了单行注释，直接结束该行\n",
    "                        break\n",
    "                    else:\n",
    "                        new_line.append(line[idx])\n",
    "                idx += 1\n",
    "            while not is_block and len(new_line) > 0:\n",
    "                ans.append(\"\".join(new_line))\n",
    "                new_line = []\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        res = []\n",
    "        new_line = []\n",
    "        in_block = False\n",
    "        for line in source:\n",
    "            i = 0\n",
    "            while i < len(line):\n",
    "                if in_block:\n",
    "                    if i + 1 < len(line) and line[i] == '*' and line[i + 1] == '/':\n",
    "                        in_block = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < len(line) and line[i] == '/' and line[i + 1] == '*':\n",
    "                        in_block = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < len(line) and line[i] == '/' and line[i + 1] == '/':\n",
    "                        break\n",
    "                    else:\n",
    "                        new_line.append(line[i])\n",
    "                i += 1\n",
    "                \n",
    "            if  not in_block and len(new_line) > 0:\n",
    "                res.append(''.join(new_line))\n",
    "                new_line = []\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        inBlock = False     #用于判断当前是否处于注释中\n",
    "        res = []\n",
    "        for line in source: #遍历所有字符串\n",
    "            i = 0\n",
    "            if not inBlock: #如果当前不在注释中，说明是新的一行，无论尾注释在哪里\n",
    "                newLine = []\n",
    "            while i < len(line):    #遍历当前行\n",
    "                if line[i:i + 2] == \"/*\" and not inBlock:   #注释起始位置\n",
    "                    inBlock = True\n",
    "                    i += 1\n",
    "                elif line[i:i + 2] == \"*/\" and inBlock:     #注释结束位置\n",
    "                    inBlock = False\n",
    "                    i += 1\n",
    "                elif line[i:i + 2] == \"//\" and not inBlock: #当前行跳过，全部注释\n",
    "                    break\n",
    "                elif not inBlock:                           #如果没有注释，则添加到新行里面\n",
    "                    newLine.append(line[i])\n",
    "                i += 1\n",
    "            if newLine and not inBlock:                     #如果新行有数据，且当前不在注释中，则更新到结果\n",
    "                res.append(\"\".join(newLine))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        res = []\n",
    "        new_line = []\n",
    "        in_block = False\n",
    "        for line in source:\n",
    "            i = 0\n",
    "            while i < len(line):\n",
    "                if in_block:\n",
    "                    if i + 1 < len(line) and line[i] == '*' and line[i + 1] == '/':\n",
    "                        in_block = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < len(line) and line[i] == '/' and line[i + 1] == '*':\n",
    "                        in_block = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < len(line) and line[i] == '/' and line[i + 1] == '/':\n",
    "                        break\n",
    "                    else:\n",
    "                        new_line.append(line[i])\n",
    "                i += 1\n",
    "                \n",
    "            if  not in_block and len(new_line) > 0:\n",
    "                res.append(''.join(new_line))\n",
    "                new_line = []\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        buf = \"\"\n",
    "        ret = []\n",
    "        in_block = False\n",
    "        for line in source: \n",
    "            tot = len(line)\n",
    "            i = 0\n",
    "            while i < tot:\n",
    "                if in_block:\n",
    "                    # try get out of the block\n",
    "                    if i + 1 < tot and line[i:i+2] == \"*/\":\n",
    "                        in_block = False\n",
    "                        i = i + 2\n",
    "                        continue\n",
    "                    # else we just ignore text\n",
    "                    i += 1\n",
    "                else:\n",
    "                    # try to get in the block\n",
    "                    if i + 1 < tot and line[i:i+2] == \"/*\":\n",
    "                        in_block = True\n",
    "                        i = i + 2\n",
    "                        continue\n",
    "                    # if we are met a line comment?\n",
    "                    if i + 1 < tot and line[i:i+2] == \"//\":\n",
    "                        break\n",
    "                    # normal text\n",
    "                    buf += line[i]\n",
    "                    i += 1\n",
    "\n",
    "            # if we are not in block and buf is not empty\n",
    "            # we push the buffer into ret\n",
    "            if len(buf) != 0 and in_block == False:\n",
    "                ret.append(buf)\n",
    "                buf = \"\"\n",
    "        \n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        ans_L = []\n",
    "        cur_l = ''\n",
    "        in_star = 0\n",
    "        for line in source:\n",
    "            ll = len(line)\n",
    "            line_finish = 0\n",
    "            i = 0\n",
    "            while i < ll and not line_finish:\n",
    "                if in_star:\n",
    "                    if line[i] != '*':\n",
    "                        i += 1\n",
    "                    elif (i+1) < ll and line[i+1] == '/':\n",
    "                        in_star = 0\n",
    "                        i += 2\n",
    "                    else:\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if line[i] != '/':\n",
    "                        cur_l += line[i]\n",
    "                        i += 1\n",
    "                    elif (i+1) < ll and line[i+1] == '/':\n",
    "                        if cur_l != '':\n",
    "                            ans_L.append(cur_l)\n",
    "                        cur_l = ''\n",
    "                        line_finish = 1\n",
    "                    elif (i+1) < ll and line[i+1] == '*':\n",
    "                        in_star = 1\n",
    "                        i += 2\n",
    "                    else:\n",
    "                        cur_l += line[i]\n",
    "                        i += 1\n",
    "            #print(cur_l)\n",
    "            if cur_l != '' and not in_star:\n",
    "                ans_L.append(cur_l)\n",
    "                cur_l = ''\n",
    "        return ans_L\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        res = [] \n",
    "        flag = False \n",
    "        currLine = \"\"  # 当前源代码行\n",
    "\n",
    "        for line in source:\n",
    "            i = 0\n",
    "            n = len(line)\n",
    "\n",
    "            while i < n:\n",
    "                if not flag and i+1 < n and line[i] == '/' and line[i+1] == '/':\n",
    "                    break  # 行注释，忽略当前字符及其右侧所有字符\n",
    "\n",
    "                elif not flag and i+1 < n and line[i] == '/' and line[i+1] == '*':\n",
    "                    flag = True  # 进入块注释\n",
    "                    i += 1\n",
    "\n",
    "                elif flag and i+1 < n and line[i] == '*' and line[i+1] == '/':\n",
    "                    flag = False  # 退出块注释\n",
    "                    i += 1\n",
    "\n",
    "                elif not flag:\n",
    "                    currLine += line[i]  # 非注释部分的字符拼接到当前行\n",
    "                i += 1\n",
    "\n",
    "            if currLine and not flag:\n",
    "                res.append(currLine)  \n",
    "                currLine = \"\"  # 重置当前行\n",
    "\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeComments(self, source: List[str]) -> List[str]:\n",
    "        res = []\n",
    "        new_line = []\n",
    "        in_block = False\n",
    "        for line in source:\n",
    "            i = 0\n",
    "            while i < len(line):\n",
    "                if in_block:\n",
    "                    if i + 1 < len(line) and line[i] == '*' and line[i+1] == '/':\n",
    "                        in_block = False\n",
    "                        i += 1\n",
    "                else:\n",
    "                    if i + 1 < len(line) and line[i] == '/' and line[i + 1] == '*':\n",
    "                        in_block = True\n",
    "                        i += 1\n",
    "                    elif i + 1 < len(line) and line[i] == '/' and line[i + 1] == '/':\n",
    "                        break\n",
    "                    else:\n",
    "                        new_line.append(line[i])\n",
    "                i += 1\n",
    "            if not in_block and len(new_line) > 0:\n",
    "                res.append(''.join(new_line))\n",
    "                new_line = []\n",
    "\n",
    "        return res\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
